//bench.js
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js')

Page({
  data: {
    selectorArray: [0, 0, 0]
  },

  onLoad: function (options) {
    console.log(options)
    let that = this;
    that.setData({
      scrollHeight:app.getScrollHeight(110)
    })
    if(options.hosText){
      that.setData({
        inputName: options.hosText
      })
      lbbService.getHospitalByKey({ key: options.hosText }, function (res) {
        that.setData({
          hospitalList: res
        })
      })
    }else {
      that.hospitalListGet()
    }
    // 请求所有省市区列表
    lbbService.getCitys({},function(res){
      that.setData({
        propArray: res
      })
    })
  },
  onReady: function () {
    // Do something when page ready.
  },
  onShow: function () {
    // Do something when page show.
  },
  onHide: function () {
    // Do something when page hide.
  },
  onUnload: function () {
    // Do something when page close.
  },
  
  myEventListener: function (e) {
    let that = this
    console.log("myEventListener>>", e)
    that.setData({
      selectorArray: e.detail
    })
    that.hospitalListGet()
  },
  searchEventListener: function (e) {
    let that = this
    console.log('searchEventListener>>', e)
    let inputName = e.detail
    let hospitalList = that.data.hospitalList
    if (!!inputName) {
      lbbService.getHospitalByKey({ key: inputName }, function (res) {
        that.setData({
          hospitalList: res
        })
      })
    }
  },
  hospitalListGet: function () {
    let that = this
    let selectorArray = that.data.selectorArray
    console.log("selectorArray>>>", selectorArray)
    // 请求指定市区的医院列表
    lbbService.getHospitalList({ array: selectorArray }, function (res) {
      that.setData({
        hospitalList: res,
      })
    })
  },
  selectHospital: function (e) {
    console.log(e)
    let pages = getCurrentPages()
    let prevPage = pages[pages.length - 2]
    let hospitalId = e.currentTarget.dataset.hospitalid
    let hospitalName = e.currentTarget.dataset.hospitalname
    let changedLocationInfo = {
      hospitalId: hospitalId,
      hospitalName: hospitalName
    }
    prevPage.setData({
      changedLocationInfo: changedLocationInfo
    })
    wx.navigateBack({
      delta: 1
    })
  },
})